<?php
require_once '../DTO/BinhLuan.php';
require_once 'DBConnection.php';

class BinhLuanDAO extends DBConnection{
	public function __construct(){
		parent::__construct();
	}
	
	//Lấy danh sách bình luận theo mã tin rao vặt
	public function getBinhLuanByMaTRV($maTRV, $index, $number ){
		$sql = "select * from binhluan where MaTRV='$maTRV' limit $index,$number";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("Failed getting binh luan");
		}
		
		if(mysql_num_rows($res)){
			$objs = array();
			$temp = mysql_fetch_assoc($res);
			while($temp){
				$objs[] = new BinhLuan($temp['MaBL'], $temp['MaND'], $temp['MaTRV'], $temp['ThoiGian'], $temp['NoiDung']);
				$temp = mysql_fetch_assoc($res);
			}
			return $objs;
		}else{
			return null;
		}
	}
	
	//Lấy số lượng tất cả bình luận theo mã tin rao vặt
	public function getSoLuongBinhLuanByMaTRV($maTRV){
		$sql = "select count(*) as SoLuong from binhluan where MaTRV='$maTRV'";
		
		$res = mysql_query($sql, $this->hDB);
		
		if(!$res){
			die("Failed getting so luong binh luan");
		}
		
		if(mysql_num_rows($res)){
			$obj = mysql_fetch_assoc($res);
			return $obj['SoLuong'];
		}else{
			return null;
		}
	}
	
	//Insert into binhluan
	public function insert(BinhLuan $binhLuan){
		$maND = $binhLuan->getMaND();
		$maTRV = $binhLuan->getMaTRV();
		$thoigian = $binhLuan->getThoiGian();
		$noidung = $binhLuan->getNoiDung();
		$sql = "insert into binhluan (MaND, MaTRV, ThoiGian, NoiDung) values ('$maND', 
		'$maTRV', '$thoigian', '$noidung')";
		
		$res = mysql_query($sql);
		
		if(!$res){
			die("Faile insert into binhluan");
		}
	}
}




